package com.ruoyi.web.controller.tongban;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tongban.domain.InsCamera;
import com.ruoyi.tongban.service.IInsCameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 视频监控Controller
 * 
 * @author caiyanshan
 * @date 2021-03-09
 */
@RestController
@RequestMapping("/tongban/camera")
public class InsCameraController extends BaseController
{
    @Autowired
    private IInsCameraService insCameraService;

    /**
     * 查询视频监控列表
     */
    @PreAuthorize("@ss.hasPermi('tongban:camera:list')")
    @GetMapping("/list")
    public TableDataInfo list(InsCamera insCamera)
    {
        startPage();
        List<InsCamera> list = insCameraService.selectInsCameraList(insCamera);
        return getDataTable(list);
    }

    /**
     * 导出视频监控列表
     */
    @PreAuthorize("@ss.hasPermi('tongban:camera:export')")
    @Log(title = "视频监控", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(InsCamera insCamera)
    {
        List<InsCamera> list = insCameraService.selectInsCameraList(insCamera);
        ExcelUtil<InsCamera> util = new ExcelUtil<InsCamera>(InsCamera.class);
        return util.exportExcel(list, "camera");
    }

    /**
     * 获取视频监控详细信息
     */
    @PreAuthorize("@ss.hasPermi('tongban:camera:query')")
    @GetMapping(value = "/{cameraId}")
    public AjaxResult getInfo(@PathVariable("cameraId") Long cameraId)
    {
        return AjaxResult.success(insCameraService.selectInsCameraById(cameraId));
    }

    /**
     * 重新生成推流地址
     */
    @GetMapping("againCreateUrl")
    public AjaxResult againCreateUrl(@RequestParam Long cameraId, @RequestParam Date loseTime)
    {
        logger.info("start update push url, the cameraId is " + cameraId);
        String loseDate = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD_HH_MM_SS, loseTime);
        return insCameraService.againCreatePushUrl(cameraId, loseDate);
    }
    
    /**
     * 新增视频监控
     */
    @PreAuthorize("@ss.hasPermi('tongban:camera:add')")
    @Log(title = "视频监控", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InsCamera insCamera)
    {
        return toAjax(insCameraService.insertInsCamera(insCamera));
    }

    /**
     * 修改视频监控
     */
    @PreAuthorize("@ss.hasPermi('tongban:camera:edit')")
    @Log(title = "视频监控", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InsCamera insCamera)
    {
        return toAjax(insCameraService.updateInsCamera(insCamera));
    }

    /**
     * 删除视频监控
     */
    @PreAuthorize("@ss.hasPermi('tongban:camera:remove')")
    @Log(title = "视频监控", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cameraIds}")
    public AjaxResult remove(@PathVariable Long[] cameraIds)
    {
        return toAjax(insCameraService.deleteInsCameraByIds(cameraIds));
    }
}
